package com.tchintech.pay.utils;

import android.app.ProgressDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.telephony.TelephonyManager;

import com.tchintech.pay.MyApplication;
import com.tchintech.pay.model.entity.HttpCode;
import com.tchintech.pay.net.DownLoadManager;

import java.io.File;

/**
 * Created by Administrator on 2017/7/25 0025.
 */

public class AppTool {

    public static void restartApplication(Context context) {
        final Intent intent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        context.startActivity(intent);
    }

    // 获取AppKey
    public static String getMetaValue(Context context, String metaKey) {
        Bundle metaData = null;
        String apiKey = null;
        if (context == null || metaKey == null) {
            return null;
        }
        try {
            ApplicationInfo ai = context.getPackageManager().getApplicationInfo(
                    context.getPackageName(), PackageManager.GET_META_DATA);
            if (null != ai) {
                metaData = ai.metaData;
            }
            if (null != metaData) {
                apiKey = metaData.getString(metaKey);
            }
        } catch (PackageManager.NameNotFoundException e) {

        }
        return apiKey;
    }
    /**
     * 获取内部版本号
     * @param paramContext
     * @return
     */
    public static int getVersionCode(Context paramContext)
    {
        try
        {
            PackageManager localPackageManager = paramContext.getPackageManager();
            String str = paramContext.getPackageName();
            int i = localPackageManager.getPackageInfo(str, 0).versionCode;
            return i;
        }
        catch (PackageManager.NameNotFoundException localNameNotFoundException)
        {

            localNameNotFoundException.printStackTrace();
            return 0;
        }
    }

    /**
     * 获取版本串号
     * @param paramContext
     * @return
     */
    public static String getVersionName(Context paramContext)
    {
        try
        {
            PackageManager localPackageManager = paramContext.getPackageManager();
            String str1 = paramContext.getPackageName();
            str1 = localPackageManager.getPackageInfo(str1, 0).versionName;
            return str1;
        }
        catch (PackageManager.NameNotFoundException localNameNotFoundException)
        {
            localNameNotFoundException.printStackTrace();
            String str2 = "unkown version name";
            return str2;
        }
    }

    /**
     * 获取手机串号
     * @param context
     * @return
     */
    public static String getImei(Context context){
        try {
            TelephonyManager tel=(TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            return  tel.getDeviceId();
        } catch (Exception e) {
            e.printStackTrace();

        }
        return "";
    }

    /**
     * 获取手机型号
     * @return
     */
    public static String getModel(){
        String mtype = android.os.Build.MODEL;
        return mtype;
    }


    public static String getPlatform(){
        return "系统版本:" + android.os.Build.VERSION.RELEASE;
    }

    public static String getManufactory(){
        return android.os.Build.MANUFACTURER;
    }

    public static float getWidthCm(int w,float d,int dpi){
        return (float) (3.0*w*1.0/(d*dpi));
    }

    /**
     * 下载apk
     */
    public static  void downLoadApk(final Handler handler, final ProgressDialog pd, final String url, final Context context) {
        pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        pd.setCanceledOnTouchOutside(false);
        pd.setMessage("正在下载更新");
        pd.show();
        new Thread() {
            @Override
            public void run() {
                try {
                    File file = DownLoadManager.getFileFromServer(
                            url, pd);
                    sleep(50);
                    AppTool.installApk(file);
                } catch (Exception e) {
                    Message msg = new Message();
                    msg.what = HttpCode.DOWN_ERROR;
                    handler.sendMessage(msg);
                    e.printStackTrace();
                }
            }
        }.start();
    }



    // 安装apk
    public static  void installApk(File file) {
        Intent intent = new Intent();
        // 执行动作
        intent.setAction(Intent.ACTION_VIEW);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        // 执行的数据类型
        intent.setDataAndType(Uri.fromFile(file),
                "application/vnd.android.package-archive");// 编者按：此处Android应为android，否则造成安装不了
        try {
            MyApplication.getInstance().startActivity(intent);
            System.exit(0);
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }
    }




    public static void startActivity(Context context){
        Intent intent = new Intent();
        intent.setComponent(new ComponentName("com.tchintech.pay","com.tchintech.pay.view.activity.AccountActivity"));
        intent.setAction(Intent.ACTION_VIEW);
        context.startActivity(intent);


    }
}
